import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../helper/cacheHelper.dart';

import '../../helper/channelHelper.dart';
import '../../helper/routeHelper.dart';
import '../../helper/uidata.dart';
import '../../model/cleanobjects.dart';
import '../../pages/cleanobjects/cleanobjects_record.dart';
import '../../pages/cleanobjects/cleanobjects_record_info.dart';

import '../../widgets/inputWidget.dart';
import '../../widgets/signatureCard2.dart';

import '../../helper/Helper.dart';
import 'cleanobjects_record2.dart';

class CleanInfoPage extends StatefulWidget {
  CleanInfoPage(this.cb1, this.added);
  final CleanObjects cb1;
  final VoidCallback added;

  @override
  CleanInfoPageState createState() => new CleanInfoPageState();
}

class CleanInfoPageState extends State<CleanInfoPage> {
  final String now = Helper.DateTimeNowString();

  bool ifLoad = false;
  late CleanObjects c1;

  @override
  void initState() {
    super.initState();
    loadData();
  }

  loadData() async {
    var xx = await CleanObjects.GetByID(widget.cb1.ID);

    setState(() {
      c1 = xx as CleanObjects;
      ifLoad = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("漂浮物运输登记单"),
      ),

      body: ifLoad
          ? CleanInfoBody(context)
          : Center(
              child: SpinKitCircle(
              color: Colors.blue,
            )),
    );
  }

  CleanInfoBody(BuildContext context) => SingleChildScrollView(
        child: CleanInfoFields(context),
      );

  String getState(CleanObjects s1) {
    String bz2 = "";
    if (s1.State == 0) bz2 = bz2 + "登记单已创建";
    if (s1.State == 1) bz2 = bz2 + "待司机确认";
    if (s1.State == 2) bz2 = bz2 + "运输中";
    if (s1.State == 3) bz2 = bz2 + "待审核";
    if (s1.State == 4) bz2 = bz2 + "已完成";
    //if (s1.State == 5) bz2 = bz2 + "河道部审核完成";

    return bz2;
  }

  String getTT(CleanObjects s1) {
    if (s1.State == 0) {
      if (UIData.IfLogin) {
        if (Helper.StrPB(UIData.CurrentUser!.Guid, s1.QMUserId)) return "起点登记";
      }
    }

    if (s1.State == 1) {
      if (UIData.cleanCar != null) {
        if (Helper.StrPB(UIData.cleanCar!.Password, s1.UPhone)) return "司机签字确认";
      }
    }

    if (s1.State == 2) {
      if (UIData.cleanCar != null) {
        if (Helper.StrPB(UIData.cleanCar!.Password, s1.UPhone)) return "终点登记";
      }
    }

    if (s1.State == 3) {
      if (UIData.IfLogin) {
        if (UIData.CurrentUser!.Authority.contains("清漂管理")) return "登记审核";
      }
    }

    // if (s1.State == 4) {
    //   if (UIData.IfLogin) {
    //     if (UIData.CurrentUser!.Authority.contains("清漂审核")) return "河道部审核";
    //   }
    // }

    return "";
  }

  void onEnd() {
    widget.added();
    Navigator.of(context).pop();
  }

  void onTapButton(BuildContext context, String xx) {
    if (xx == "起点登记") {
      RouteHelper.ShowPage(context, CleanRecordPage(c1, onEnd));
    }

    if (xx == "终点登记") {
      RouteHelper.ShowPage(context, CleanRecordPage2(c1, onEnd));
    }

    if (xx == "司机签字确认") {
      showPanel(context, signIt1, xx);
    }

    if (xx == "登记审核") {
      showPanel(context, signIt2, xx);
    }

    // if (xx == "河道部审核") {
    //     showPanel(context, signIt3, xx);
    // }
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list5 = <Widget>[];

    list3.add(InputWidget.getInfo("车牌号", c1.CarCode));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInfo("运载量", c1.Volume.toString()));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInfo("司机姓名", c1.UName.toString()));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getUserTile2(
        context, "联系电话", c1.UPhone.toString(), c1.UPhone.toString()));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInfo("运输地点", c1.Location));

    list4.add(InputWidget.getInfo("当前状态", getState(c1)));
    list4.add(InputWidget.getDivider());
    if (c1.State >= 1)
      list4.add(InputWidget.getInfo("起点登记", Helper.DateTimeFormat(c1.TM1),
          onTap: () => RouteHelper.ShowPage(
              context, CleanRecordInfoPage("起点登记", c1.Gps1, c1.Img1))));
    list4.add(InputWidget.getDivider());

    if (c1.State >= 3)
      list4.add(InputWidget.getInfo("终点登记", Helper.DateTimeFormat(c1.TM2),
          onTap: () => RouteHelper.ShowPage(
              context, CleanRecordInfoPage("终点登记", c1.Gps2, c1.Img2))));

    if (c1.State >= 2) {
      list2.add(InputWidget.getDivider());
      list2.add(InputWidget.getQMTile2("司机签字确认", true, c1.UQM, c1.UQMTM));
    }

    if (c1.State >= 4) {
      list2.add(InputWidget.getDivider());
      list2.add(InputWidget.getQMTile2("登记单审核", true, c1.SYQM, c1.SYQMTM));
    }

    // if (c1.State >= 5) {
    //   list2.add(InputWidget.getDivider());
    //   list2.add(InputWidget.getQMTile2(
    //       "河道部审核", true, c1.HDQM, c1.HDQMTM));
    // }

    list1.add(InputWidget.inCard2(list3));
    list1.add(InputWidget.inCard2(list4));
    list1.add(InputWidget.inCard2(list2));
    var x1 = getTT(c1);
    if (x1 != "")
      list5.add(InputWidget.getButton(x1, () => onTapButton(context, x1)));

    list1.add(InputWidget.inCard2(list5));
    return list1;
  }

  signIt1(String imgId) async {
    c1.UIfQM = true;
    c1.UQMTM = Helper.DateTimeNowString();
    c1.UQM = imgId;
    c1.State = 2;
    await CleanObjects.Update(c1);
    onEnd();
    //await Helper.sendMessage("审核通知", "18884230190");
  }

  signIt2(String imgId) async {
    c1.IfSYQM = true;
    c1.SYQMTM = Helper.DateTimeNowString();
    c1.SYQM = imgId;
    c1.State = 4;
    await CleanObjects.Update(c1);
    onEnd();
  }

  signIt3(String imgId) async {
    c1.IfHDQM = true;
    c1.HDQMTM = Helper.DateTimeNowString();
    c1.HDQM = imgId;
    c1.State = 5;
    //c1.HDUserId = UIData.CurrentUser!.Guid;

    await CleanObjects.Update(c1);
    onEnd();
    //await Helper.sendMessage("审核通知", "18884230190");
  }

  void showPanel(
      BuildContext context, SignaturedCallback checkIt, String title) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SignatureCard2Page(checkIt, title);
      },
    );
  }

  CleanInfoFields(BuildContext context) => Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: getWidgetList(context),
        ),
      );
}
